package botcraft;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

public class Configuration {
	private static final String LOCALE = "locale";
	private static final String SCREEN = "screen";
	private static final String DISPLAY_MODE_WIDTH = "displayModeWidth";
	private static final String DISPLAY_MODE_HEIGHT = "displayModeHeight";
	private static final String DISPLAY_MODE_BIT_DEPTH = "displayModeBitDepth";
	private static final String DISPLAY_MODE_REFRESH_RATE = "displayModeRefreshRate";
	private static final String RESOURCE_BUNDLE = "i18n/botcraft";

	private final Preferences preferences;
	private Locale locale;
	private GraphicsDevice screen;
	private DisplayMode displayMode;
	
	
	public Configuration() {
		preferences = Preferences.userNodeForPackage(Configuration.class);
		locale = null;
		final Locale[] locales = getLocales();
		for (Locale locale : locales) {
			if (locale.getLanguage().equals(preferences.get(LOCALE, null)))
				this.locale = locale;
		}
		if (locale == null)
			locale = Arrays.asList(locales).contains(Locale.getDefault()) ? Locale.getDefault() : ((locales.length > 0) ? locales[0] : null);
		screen = null;
		final GraphicsDevice[] screens = getScreens();
		for (GraphicsDevice screen : screens) {
			if (screen.getIDstring().equals(preferences.get(SCREEN, null)))
				this.screen = screen;
		}
		if (screen == null)
			screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		displayMode = null;
		for (DisplayMode displayMode : getDisplayModes()) {
			if ((displayMode.getWidth() == preferences.getInt(DISPLAY_MODE_WIDTH, 0)) && (displayMode.getHeight() == preferences.getInt(DISPLAY_MODE_HEIGHT, 0)) &&
					(displayMode.getBitDepth() == preferences.getInt(DISPLAY_MODE_BIT_DEPTH, 0)) && (displayMode.getRefreshRate() ==
					preferences.getInt(DISPLAY_MODE_REFRESH_RATE, 0)))
				this.displayMode = displayMode;
		}
		if (displayMode == null)
			displayMode = getScreen().getDisplayMode();

	}
		
	public void save() throws BackingStoreException {
		preferences.put(LOCALE, locale.getLanguage());
		preferences.put(SCREEN, screen.getIDstring());
		preferences.putInt(DISPLAY_MODE_WIDTH, displayMode.getWidth());
		preferences.putInt(DISPLAY_MODE_HEIGHT, displayMode.getHeight());
		preferences.putInt(DISPLAY_MODE_BIT_DEPTH, displayMode.getBitDepth());
		preferences.putInt(DISPLAY_MODE_REFRESH_RATE, displayMode.getRefreshRate());
		preferences.flush();
	}
	
	public Locale[] getLocales() {
		final Set<Locale> locales = new HashSet<Locale>();
		for (Locale locale : Locale.getAvailableLocales()) {
			if (ResourceBundle.getBundle(RESOURCE_BUNDLE, locale).getLocale().equals(locale))
				locales.add(locale);
		}
		return locales.toArray(new Locale[0]);
	}
	
	public Locale getLocale() {
		return locale;
	}
	
	public void setLocale(final Locale locale) {
		this.locale = locale;
	}
	
	public GraphicsDevice[] getScreens() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
	}

	public GraphicsDevice getScreen() {
		return screen;
	}
		
	public void setScreen(final GraphicsDevice screen) {
		this.screen = screen;
	}
	
	public DisplayMode[] getDisplayModes() {
		return screen.getDisplayModes();
	}

	public DisplayMode getDisplayMode() {
		return displayMode;
	}
	
	public void setDisplayMode(final DisplayMode displayMode) {
		this.displayMode = displayMode;
	}
}

